///
/// 张诚宇
///
/// 视频详情页面
///
/// 2022.6.14
///
///

import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter_barrage/flutter_barrage.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:video_player/video_player.dart';
import 'package:zhangchengyu20220614/model/VideoListEntity.dart';
import 'package:zhangchengyu20220614/view/login.dart';

class VideoMessagePage extends StatefulWidget {
  final Result result;
  const VideoMessagePage({Key key,this.result}) : super(key: key);

  @override
  _VideoMessagePageState createState() => _VideoMessagePageState();
}

class _VideoMessagePageState extends State<VideoMessagePage> {

  VideoPlayerController _videoPlayerController;
  ChewieController _chewieController;
  BarrageWallController _barrageWallController;
  TextEditingController _textEditingController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _videoPlayerController=VideoPlayerController.network(widget.result.videoUrl);
    _chewieController=ChewieController(videoPlayerController: _videoPlayerController,aspectRatio: 3/3,autoInitialize: true,autoPlay: true);
    _barrageWallController=BarrageWallController();
    _textEditingController = TextEditingController();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _chewieController.pause();
    _chewieController.dispose();
    _videoPlayerController.dispose();
    _barrageWallController.dispose();
    _textEditingController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _barrageWallController == null || _chewieController==null ? Center(child: CircularProgressIndicator(color: Colors.deepOrange,),) : Column(
        children: [
          Expanded(
            flex: 9,
            child: AspectRatio(
              aspectRatio: _chewieController.aspectRatio,
              child: BarrageWall(
                  controller: _barrageWallController,child: Chewie(controller: _chewieController,)),
            ),
          ),
          Expanded(
            flex: 1,
            child: Expanded(
              child: TextField(
                controller: _textEditingController,
                onSubmitted: (str){
                  isLogin();
                },
                decoration: InputDecoration(
                  hintText: '写评论...'
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  void isLogin() async{
    var sharedPreferences = await SharedPreferences.getInstance();
    var bool = sharedPreferences.getBool('isLogin');
    if(bool==null){
      _chewieController.dispose();
      Navigator.push(context, MaterialPageRoute(builder: (context)=>LoginPage()));
      Fluttertoast.showToast(msg: '请先登录');
    }else{
      _barrageWallController.send([Bullet(child: Text(_textEditingController.text,style: TextStyle(color: Colors.blueAccent),))]);
      _textEditingController.clear();
    }
  }
}
